var maxCount = Number(getQueryString('goodsCount'));//获取添加的数量
var firstCount = Number(getQueryString('goodsCount'));//记录最初的数量
function addGoods() {
    /*获取table的行*/
    var trArr = $("#datatable tr");
    var selectGoodsLists = [];
    if(sessionStorage.getItem("goodsTr")){
        var goodsTr = JSON.parse(sessionStorage.getItem("goodsTr"));
    }else {
        var goodsTr = [];//选中的商品数据
    }
    if(goodsTr.length == 0){
        for(var i=0;i<trArr.length;i++){
            if($(trArr[i]).find("td")[0].firstElementChild.checked == true){
                selectGoodsLists.push(trArr[i]);
                goodsTr.push({
                    sku_code: $(trArr[i]).find(".sku-code").html(),
                    sku_name: $(trArr[i]).find(".sku-name").html(),
                    sale_attri_value: $(trArr[i]).find(".sale-attri-value").html(),
                    origin: $(trArr[i]).find(".origin").html(),
                    count: $(trArr[i]).find(".count").find(".numberbar").find("input").val(),
                    unit: $(trArr[i]).find(".unit").html(),
                    parts_number: $(trArr[i]).find(".parts-number").html(),
                    mini_order: $(trArr[i]).find(".mini-order").html(),
                    saleType: Number($(trArr[i]).find(".sale-type-code").html()),
                    price: $(trArr[i]).find(".price").html(),
                    sub_totle: $(trArr[i]).find(".sub-total").html(),
                    actual_stock: $(trArr[i]).find(".actual-stock").html(),
                    virtual_stock: $(trArr[i]).find(".virtual-stock").html(),
                    arrival_cycle: $(trArr[i]).find(".arrival-cycle").html(),
                    warehouse_name: $(trArr[i]).find(".warehouse-name").html(),
                    helpcode: $(trArr[i]).find(".help-code").html(),
                    spu_id: $(trArr[i]).find(".spu-id").html(),
                    sku_id: $(trArr[i]).find(".sku-id").html(),
                    order_limit_type:$(trArr[i]).find(".order-limit-type").html(),
                    checkedState: true,
                });
            }
        }
    }else {
        for(var i=0;i<trArr.length;i++){
            if($(trArr[i]).find("td")[0].firstElementChild.checked == true){
                selectGoodsLists.push(trArr[i]);
                var repeat = false;
                for(var j=0;j<goodsTr.length;j++){
                    if(goodsTr[j].sku_code == $(trArr[i]).find(".sku-code").html()){
                        goodsTr[j].count = $(trArr[i]).find(".count").find(".numberbar").find("input").val();
                        goodsTr[j].sub_totle = $(trArr[i]).find(".sub-totle").html();
                        repeat = true;
                        continue;
                    }
                }
                if(repeat == false){
                    goodsTr.push({
                        sku_code: $(trArr[i]).find(".sku-code").html(),
                        sku_name: $(trArr[i]).find(".sku-name").html(),
                        sale_attri_value: $(trArr[i]).find(".sale-attri-value").html(),
                        origin: $(trArr[i]).find(".origin").html(),
                        count: $(trArr[i]).find(".count").find(".numberbar").find("input").val(),
                        unit: $(trArr[i]).find(".unit").html(),
                        parts_number: $(trArr[i]).find(".parts-number").html(),
                        mini_order: $(trArr[i]).find(".mini-order").html(),
                        saleType: Number($(trArr[i]).find(".sale-type-code").html()),
                        price: $(trArr[i]).find(".price").html(),
                        sub_totle: $(trArr[i]).find(".sub-totle").html(),
                        actual_stock: $(trArr[i]).find(".actual-stock").html(),
                        virtual_stock: $(trArr[i]).find(".virtual-stock").html(),
                        arrival_cycle: $(trArr[i]).find(".arrival-cycle").html(),
                        warehouse_name: $(trArr[i]).find(".warehouse-name").html(),
                        helpcode: $(trArr[i]).find(".help-code").html(),
                        spu_id: $(trArr[i]).find(".spu-id").html(),
                        sku_id: $(trArr[i]).find(".sku-id").html(),
                        order_limit_type:$(trArr[i]).find(".order-limit-type").html(),
                        checkedState: true,
                    });
                }
            }
        }
    }
    //获取选中的数据
    if(selectGoodsLists.length == 0){
        $.Huimodalalert("未选择商品",2000);
    }else {
        sessionStorage.setItem("goodsTr",JSON.stringify(goodsTr));
        window.parent.renderTable(false);
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
}
function goBack() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
/*获取搜索参数*/
var skuId =Base64.decode(getQueryString('skuId'));
var skuName =Base64.decode(getQueryString('skuName'));
var helpCode =Base64.decode(getQueryString('helpCode'));
var cityId =getQueryString('cityId');
var customerId = getQueryString('customerId');
var warehouseCode = getQueryString('warehouseCode');
/*搜索该搜索参数下的商品*/
$.ajax({
    url: root.replaceInter.replace_skuSubstituteList,
    cache: false, //禁用缓存
    type: "POST",
    data: {
        "skuCode":skuId,
        "skuName":skuName,
        "helpCode":helpCode,
        "cityId":cityId,
        "customerId":customerId,
        "warehouseCode":warehouseCode
    },
    success: function(data) {
        if(data.errorCode == 0){
            if(data.data.length == 0){
                window.parent.noGoods();
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            }else{
                renderTable(data);
            }
        }
    }
});
/*获取分类树*/
$.ajax({
    url: root.replaceInter.replace_getAllCategory,
    type: "get",
    success: function(data) {
        if(data.errorCode == 0){
            var zNodes = [{
                category_name:"鑫方盛集团",
                category_code:"all",
                childList:data.data
            }];
            /*分类树渲染*/
            var setting = {
                data: {
                    key: {
                        name: "category_name",
                        children: "childList"
                    },
                    simpleData: {
                        enable: false,
                        idKey: "category_id",
                    }
                },
                callback: {
                    onClick: getQueryData
                }
            };
            function getQueryData(e, treeDemo, treeNode) {
                /*搜索品类参树下的商品sku*/
                $.ajax({
                    url: root.replaceInter.replace_getCategoryChildSku,
                    cache: false, //禁用缓存
                    type: "POST",
                    data: {
                        "skuCode":skuId,
                        "skuName":skuName,
                        "helpCode":helpCode,
                        "categoryCode": treeNode.category_code,
                        "cityId":cityId,
                        "customerId":customerId,
                        "warehouseCode":warehouseCode
                    },
                    success: function(data) {
                        if(data.errorCode == 0){
                            if(data.data.length == 0){
                                maxCount = firstCount;
                                $("#checkAll")[0].checked = false;
                                $("#datatable tbody").html("");
                                $.Huimodalalert("无搜索结果",2000);
                            }else{
                                maxCount = firstCount;
                                $("#checkAll")[0].checked = false;
                                renderTable(data);
                            }

                        }
                    }
                });
            }

            $(document).ready(function(){
                var t = $("#treeDemo");
                t = $.fn.zTree.init(t, setting, zNodes);
                var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                var nodes = t.getNodes();
                treeObj.expandNode(nodes[0], true, false, false);
            });
        }
    }
});
/*sale_type
10 指定展示价
20 指定交易价
30 指定展示价和指定交易价
40 批发价
50 分销价*/
// 指定价判断
function getSaleType(saleTypeCode) {
    switch (saleTypeCode){
        case 10:
            return "指定展示价"
            break;
        case 20:
            return "指定交易价"
            break;
        case 30:
            return "指定展示价和指定交易价"
            break;
        case 40:
            return "批发价"
            break;
        case 50:
            return "分销价"
            break;
        default:
            return ""
            break;
    }
}
/*渲染table表格*/
function renderTable(data){
    var goodsSkuStr = "";
    for(var i=0;i<data.data.length;i++){
        // 预计出货日计算
        if(data.data[i].actual_stock>=data.data[i].mini_order){
            var arrivalCycle = 1;
        } else {
            var arrivalCycle = data.data[i].arrival_cycle+1;
        }
        // 缺货数量计算
        if(Number(data.data[i].actual_stock)>=Number(data.data[i].mini_order)){
            var littleNumber = "--";
        }else {
            var littleNumber = Number(data.data[i].mini_order)-Number(data.data[i].actual_stock);

        }

        // 表格节点字符串
        goodsSkuStr += '<tr onclick="selectParentTr(this)">' +
            '<td onclick="window.event? window.event.cancelBubble = true : e.stopPropagation();"><input class="spuId" type="checkbox" onchange="selectTr(this)" name="spuId" value="'+i+'"><span class="num">'+(i+1)+'</span></td>' +
            '<td class="sku-code 1">'+dealNull(data.data[i].sku_code)+'</td>' +
            '<td class="sku-name 2">'+dealNull(data.data[i].sku_name)+'</td>' +
            '<td class="sale-attri-value 3">'+dealNull(data.data[i].sale_attri_value)+" "+dealNull(data.data[i].color)+'</td>' +
            '<td style="display: none;" class="origin 4">'+dealNull(data.data[i].origin)+'</td>' +
            '<td class="count 5"><div class="numberbar"><span class="number_decrease">-</span><span class="number_increase">+</span><input onclick="window.event? window.event.cancelBubble = true : e.stopPropagation();" type="text" oninput="checkCount(this)" class="num_text" value="'+data.data[i].mini_order+'"></div></td>' +
            '<td class="unit 6">'+dealNull(data.data[i].unit)+'</td>' +
            '<td class="parts-number 7">'+dealNull(data.data[i].parts_number)+'</td>' +
            '<td class="mini-order 8">'+dealNull(data.data[i].mini_order)+'</td>' +
            '<td class="sale-type 9">'+getSaleType(data.data[i].sale_type)+'</td>' +
            '<td class="price 10">'+Number(data.data[i].sale_price).toFixed(2)+'</td>' +
            '<td class="sub-total 11">'+(Number(data.data[i].sale_price)*Number(data.data[i].mini_order)).toFixed(2)+'</td>' +
            '<td class="actual-stock 12">'+dealNull(data.data[i].actual_stock)+'</td>' +
            '<td class="little-number 13">'+littleNumber+'</td>' +
            '<td class="virtual-stock 14">'+dealNull(data.data[i].virtual_stock)+'</td>' +
            '<td class="arrivalCycle">'+arrivalCycle+'</td>' +
            '<td class="warehouse-name 16">'+dealNull(data.data[i].warehouse_name)+'</td>' +
            '<td style="display:none;" class="help-code 17">'+dealNull(data.data[i].helpcode)+'</td>' +
            '<td style="display:none;" class="spu-id 18">'+dealNull(data.data[i].spu_id)+'</td>' +
            '<td style="display:none;" class="sku-id 19">'+dealNull(data.data[i].sku_id)+'</td>' +
            '<td style="display:none;" class="order-limit-type 20">'+dealNull(data.data[i].order_limit_type)+'</td>' +
            '<td style="display:none;" class="arrival-cycle 21">'+dealNull(data.data[i].arrival_cycle)+'</td>' +
            '<td style="display:none;" class="sale-type-code 22">'+data.data[i].sale_type+'</td>' +
            '</tr>'
    }
    $("#datatable tbody").html(goodsSkuStr);
    /*判断库存是否为0，为0时标红*/
    var trArr = $("#datatable tr");
    for(var i=0;i<trArr.length;i++){
        // 库存
        if($(trArr[i]).find(".price").html() <= 0){
            $(trArr[i]).css({
                background:"#ffe7e7"
            })
        }
        // 缺货数量
        if($(trArr[i]).find(".actual-stock").html() > 0){
            $(trArr[i]).find(".actual-stock")[0].style.color = "red";
        }
    }

    /*计数器功能*/
    $(".number_increase").click(function () {
        //计数器功能加
        window.event? window.event.cancelBubble = true : e.stopPropagation();
        this.parentElement.firstChild.style.background = "white";
        if(this.parentElement.lastChild.value<Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html())+Number($(this.parentElement.parentElement.parentElement).find(".virtual-stock").html())){
            if($(this.parentElement.parentElement.parentElement).find(".order-limit-type").html()==1){
                this.parentElement.lastChild.value = Number(this.parentElement.lastChild.value)+1;
                if(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= this.parentElement.lastChild.value){
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html("--");
                    $(this.parentElement.parentElement.parentElement).find(".little-number")[0].style.color = "#333";
                }else {
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html(this.parentElement.lastChild.value - Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()));
                    $(this.parentElement.parentElement.parentElement).find(".little-number")[0].style.color = "red";
                }
            }else {
                // 最小起订量和步长值判断
                if(this.parentElement.lastChild.value<(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html())+Number($(this.parentElement.parentElement.parentElement).find(".virtual-stock").html()))-((Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html())+Number($(this.parentElement.parentElement.parentElement).find(".virtual-stock").html()))%Number($(this.parentElement.parentElement.parentElement).find(".mini-order").html()))){
                    this.parentElement.lastChild.value = Number(this.parentElement.lastChild.value)+Number($(this.parentElement.parentElement.parentElement).find(".mini-order").html());
                }else{
                    $(this.parentElement).find(".number_increase")[0].style.background = "#c3c3c3";
                }
                if(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= this.parentElement.lastChild.value){
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html("--");
                    $(this.parentElement.parentElement.parentElement).find(".little-number")[0].style.color = "#333";
                }else {
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html(this.parentElement.lastChild.value - Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()));
                    $(this.parentElement.parentElement.parentElement).find(".little-number")[0].style.color = "red";
                }
            }
        }else{
            $(this.parentElement).find(".number_increase")[0].style.background = "#c3c3c3";
            this.parentElement.lastChild.value = (Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html())+Number($(this.parentElement.parentElement.parentElement).find(".virtual-stock").html()))-((Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html())+Number($(this.parentElement.parentElement.parentElement).find(".virtual-stock").html()))%Number($(this.parentElement.parentElement.parentElement).find(".mini-order").html()));
            if(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= this.parentElement.lastChild.value){
                $(this.parentElement.parentElement.parentElement).find(".little-number").html("--");
                $(this.parentElement.parentElement.parentElement).find(".little-number").style.color = "#333";
            }else {
                $(this.parentElement.parentElement.parentElement).find(".little-number").html(this.parentElement.lastChild.value - Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()));
                $(this.parentElement.parentElement.parentElement).find(".little-number").style.color = "red";
            }
        }
        var thisCount = Number(this.parentElement.lastChild.value);//数量
        var thisPrice = Number($(this.parentElement.parentElement.parentElement).find(".price").html()); //单价
        // 预计出货日
        if(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html())>=thisCount){
            $(this.parentElement.parentElement.parentElement).find(".arrivalCycle").html(1);
        } else {
            $(this.parentElement.parentElement.parentElement).find(".arrivalCycle").html(Number($(this.parentElement.parentElement.parentElement).find(".arrival-cycle").html())+1);
        }
        subtotal(thisCount,thisPrice,this);//小计计算
    });
    $(".number_decrease").click(function () {
        // 计数器功能减
        window.event? window.event.cancelBubble = true : e.stopPropagation();
        if(Number(this.parentElement.lastChild.value) > Number($(this.parentElement.parentElement.parentElement).find(".mini-order").html())){
            this.parentElement.firstChild.style.background = "white";
            $(this.parentElement).find(".number_increase")[0].style.background = "white";
            if($(this.parentElement.parentElement.parentElement).find(".order-limit-type").html()==1){
                this.parentElement.lastChild.value = Number(this.parentElement.lastChild.value)-1;
                if(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= this.parentElement.lastChild.value){
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html("--");
                    $(this.parentElement.parentElement.parentElement).find(".little-number").style.color = "#333";
                }else {
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html(this.parentElement.lastChild.value - Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()));
                    $(this.parentElement.parentElement.parentElement).find(".little-number").style.color = "red";
                }
            }else {
                this.parentElement.lastChild.value = Number(this.parentElement.lastChild.value)-Number($(this.parentElement.parentElement.parentElement).find(".mini-order").html());
                if(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= this.parentElement.lastChild.value){
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html("--");
                    $(this.parentElement.parentElement.parentElement).find(".little-number").style.color = "#333";
                }else {
                    $(this.parentElement.parentElement.parentElement).find(".little-number").html(this.parentElement.lastChild.value - Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html()));
                    $(this.parentElement.parentElement.parentElement).find(".little-number").style.color = "red";
                }
            }
        } else {
            this.parentElement.firstChild.style.background = "#c3c3c3";
        }
        var thisCount = Number(this.parentElement.lastChild.value); //数量
        // 预计出货日
        if(Number($(this.parentElement.parentElement.parentElement).find(".actual-stock").html())>=thisCount){
            $(this.parentElement.parentElement.parentElement).find(".arrivalCycle").html(1);
        } else {
            $(this.parentElement.parentElement.parentElement).find(".arrivalCycle").html(Number($(this.parentElement.parentElement.parentElement).find(".arrival-cycle").html())+1);
        }
        var thisPrice = Number($(this.parentElement.parentElement.parentElement).find(".price").html());//单价
        subtotal(thisCount,thisPrice,this);//小计计算
    });
}

/*校验库存及手动修改计数器时小计的计算*/
function checkCount(el){
    el.value=el.value.replace(/[^0-9]/ig,'');
    if(el.value == 0||el.value%Number($(el.parentElement.parentElement.parentElement).find(".mini-order").html())!=0){
        if(el.value == 0){
            el.value = Number($(el.parentElement.parentElement.parentElement).find(".mini-order").html());
        }else {
            el.value = Number($(el.parentElement.parentElement.parentElement).find(".mini-order").html())-(el.value%Number($(el.parentElement.parentElement.parentElement).find(".mini-order").html()));
        }

        if(Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= el.value){
            $(el.parentElement.parentElement.parentElement).find(".little-number").html("--");
            $(el.parentElement.parentElement.parentElement).find(".little-number").style.color = "#333";
        }else {
            $(el.parentElement.parentElement.parentElement).find(".little-number").html(el.value - Number($(el.parentElement.parentElement.parentElement).find(".sub-total").html()));
            $(this.parentElement.parentElement.parentElement).find(".little-number").style.color = "red";
        }
    }
    if(el.value>Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html())+Number($(el.parentElement.parentElement.parentElement).find(".virtual-stock").html())){
        el.value = Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html())+Number($(el.parentElement.parentElement.parentElement).find(".virtual-stock").html());
        $(el.parentElement.parentElement.parentElement).find(".sub-total").html((Number(el.value)*Number($(el.parentElement.parentElement.parentElement).find(".price").html())).toFixed(2));
        if(Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= el.value){
            $(el.parentElement.parentElement.parentElement).find(".little-number").html("--");
            $(el.parentElement.parentElement.parentElement).find(".little-number").style.color = "#333";
        }else {
            $(el.parentElement.parentElement.parentElement).find(".little-number").html(el.value - Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html()));
            $(el.parentElement.parentElement.parentElement).find(".little-number").style.color = "red";
        }
    }else {
        $(el.parentElement.parentElement.parentElement).find(".sub-total").html((Number($(el.parentElement.parentElement.parentElement).find(".count")[0].firstChild.lastChild.value)*Number($(el.parentElement.parentElement.parentElement).find(".price").html())).toFixed(2));
        if(Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html()) >= Number($(el.parentElement.parentElement.parentElement).find(".count")[0].firstChild.lastChild.value)){
            $(el.parentElement.parentElement.parentElement).find(".little-number").html("--");
            $(el.parentElement.parentElement.parentElement).find(".little-number").style.color = "#333";
        }else {
            $(el.parentElement.parentElement.parentElement).find(".little-number").html(Number($(el.parentElement.parentElement.parentElement).find(".count")[0].firstChild.lastChild.value) - Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html()));
            $(el.parentElement.parentElement.parentElement).find(".little-number").style.color = "red";
        }

    }
    // 预计出货日
    if(Number($(el.parentElement.parentElement.parentElement).find(".actual-stock").html())>=el.value){
        $(el.parentElement.parentElement.parentElement).find(".arrivalCycle").html(1);
    } else {
        $(el.parentElement.parentElement.parentElement).find(".arrivalCycle").html(Number($(el.parentElement.parentElement.parentElement).find(".arrival-cycle").html())+1);
    }
}
/*小计计算*/
function subtotal(count,price,el) {
    $(el.parentElement.parentElement.parentElement).find(".sub-total").html((count*price).toFixed(2));
}
/*检验商品是否重复添加*/
function selectTr(el) {
    var goodsTr = JSON.parse(sessionStorage.getItem("goodsTr"));
    for(var j=0;j<goodsTr.length;j++){
        if(goodsTr[j].sku_code == $(el.parentElement.parentElement).find(".sku-code").html()){
            console.log(goodsTr[j].sku_code,$(el.parentElement.parentElement).find(".sku-code").html())
            maxCount -=1;
        }
    }
    if(Number($(el.parentElement.parentElement).find(".actual-stock").html())+Number($(el.parentElement.parentElement).find(".virtual-stock").html()) <= 0||Number($(el.parentElement.parentElement).find(".actual-stock").html())+Number($(el.parentElement.parentElement).find(".virtual-stock").html()) <= Number($(el.parentElement.parentElement).find(".mini-order").html())){
        $.Huimodalalert("库存不足，不能添加该商品",2000);
        el.checked = false;
    }else {
        if (el.checked == true){
            maxCount += 1;
            if(maxCount>999){
                $.Huimodalalert("最多添加999种商品",2000);
                el.checked = false;
                maxCount = 999;
            }
        }else {
            maxCount -= 1;
        }
    }

}
/*点击tr选中当前tr的CheckBox*/
function selectParentTr(el) {
    console.log(el)
    var goodsTr = JSON.parse(sessionStorage.getItem("goodsTr"));
    for(var j=0;j<goodsTr.length;j++){
        if(goodsTr[j].sku_code == $(el).find(".sku-code").html()){
            console.log(goodsTr[j].sku_code,$(el).find(".sku-code").html())
            maxCount -=1;
        }
    }
    if(Number($(el).find(".actual-stock").html())+Number($(el).find(".virtual-stock").html()) <= 0||Number($(el).find(".actual-stock").html())+Number($(el).find(".virtual-stock").html())<Number($(el).find(".mini-order").html())){
        $.Huimodalalert("库存不足，不能添加该商品",2000);
        $(el).find(".spuId")[0].checked = false;
    }else {
        if($(el).find(".spuId")[0].checked == false){
            $(el).find(".spuId")[0].checked = true;
            maxCount += 1;
        }else {
            $(el).find(".spuId")[0].checked = false;
            maxCount -= 1;
        }
    }
    if(maxCount>999){
        $.Huimodalalert("最多添加999种商品",2000);
        $(el).find(".spuId")[0].checked = false;
        maxCount = 999;
    }
}
/*全选功能*/
function selectAll(el) {
    var goodsTr = JSON.parse(sessionStorage.getItem("goodsTr"));

    var selectAllInp = document.querySelectorAll(".spuId");
    if(el.checked == true){
        for(var i=0;i<selectAllInp.length;i++){
            var thisTr = selectAllInp[i].parentElement.parentElement;
            console.log(Number($(thisTr).find(".actual-stock").html())+Number($(thisTr).find(".virtual-stock").html()))
            console.log(Number($(thisTr).find(".mini-order").html()))
            if(Number($(thisTr).find(".actual-stock").html())+Number($(thisTr).find(".virtual-stock").html())>=Number($(thisTr).find(".mini-order").html())){
                maxCount+=1;
                selectAllInp[i].checked = el.checked;
            }
            for(var j=0;j<goodsTr.length;j++){
                if(goodsTr[j].sku_code == $(thisTr).find(".sku-code").html()){
                    maxCount-=1;
                    selectAllInp[i].checked = el.checked;
                }
            }
        }
    }else {
        maxCount = firstCount;
        for(var i=0;i<selectAllInp.length;i++){
            var thisTr = selectAllInp[i].parentElement.parentElement;
            selectAllInp[i].checked = el.checked;
        }
    }

    
    if(maxCount>999){
        $.Huimodalalert("最多添加999种商品",2000);
        el.checked = false;
        maxCount = Number(getQueryString('goodsCount'));//获取添加的数量
        for(var i=0;i<selectAllInp.length;i++){
                selectAllInp[i].checked = false;
        }
    }
}